package com.cuz.hot100;

import com.cuz.daileetcode.utils.ArrayUtils;

public class Q73setZeroes {
    public static void main(String[] args) {
        // [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
        //[[0,1,2,0],[3,4,5,2],[1,3,1,5]]
        int[][] ints = {
                {0, 1, 2, 0},
                {3, 4, 5, 2},
                {1, 3, 1, 5}};
        ArrayUtils.print(ints);
        setZeroes1(ints);
        System.out.println();
        ArrayUtils.print(ints);
    }

    public static void setZeroes1(int[][] matrix) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return;
        }
        //第一行是否有0
        boolean hasZeroRow0 = false;
        //第一列是否有0
        boolean hasZeroCol0 = false;
        int colMax = matrix[0].length;
        int rowMax = matrix.length;
        for (int[] ints : matrix) {
            if (ints[0] == 0) {
                hasZeroCol0 = true;
                break;
            }
        }
        for (int i = 0; i < colMax; i++) {
            if (matrix[0][i] == 0) {
                hasZeroRow0 = true;
                break;
            }
        }
        for (int row = 1; row < rowMax; row++) {
            for (int col = 1; col < colMax; col++) {
                if (matrix[row][col] == 0) {
                    matrix[row][0] = 0;
                    matrix[0][col] = 0;
                }
            }
        }
        for (int row = 1; row < rowMax; row++) {
            for (int col = 1; col < colMax; col++) {
                if (matrix[row][0] == 0 || matrix[0][col] == 0) {
                    matrix[row][col] = 0;
                }
            }
        }
        if (hasZeroCol0) {
            for (int i = 0; i < rowMax; i++) {
                matrix[i][0] = 0;
            }
        }
        if (hasZeroRow0) {
            for (int i = 0; i < colMax; i++) {
                matrix[0][i] = 0;
            }
        }
    }
}
